package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.Generalize;
import com.wude.bean.UserInfo;
import com.wude.guild.ActivityRules;
import com.wude.guild.GameDetActivity;
import com.wude.guild.LoginActivity;
import com.wude.guild.MyInvitationActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.GeneralizeAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 推广
 */
@SuppressLint("HandlerLeak")
public class GeneralizeFragment extends TitleBaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private TextView tv_activityDetail;

    private ListView lv_generalize;
    private GeneralizeAdapter adapter;
    private SpringView springview;
    private int limit = 1;
    private ArrayList<Generalize> dataList = new ArrayList<>();

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.generalize_fragment, container, false);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("GeneralizeFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHeaderTitle("分享有礼");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("明细");
        rightTextView.setOnClickListener(this);
        getTitleHeaderBar().getLeftImageView().setVisibility(View.GONE);
        initView(view);
    }

    private void initView(View view) {
        tv_activityDetail = findView(view, R.id.tv_activityDetail);
        lv_generalize = view.findViewById(R.id.lv_generalize);
        springview = view.findViewById(R.id.springview);

        adapter = new GeneralizeAdapter(getActivity());
        lv_generalize.setAdapter(adapter);
        lv_generalize.setOnItemClickListener(this);
        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
//        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));

        tv_activityDetail.setOnClickListener(this);
        initAndReflsh();
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };

    private void initAndReflsh() {
        dataList.clear();
        limit = 1;
        Map<String, String> map = new HashMap<>();
        map.put("sdk_version", "1");
        map.put("limit", limit + "");
        HttpCom.POST(handler, HttpCom.getGeneralize(), map, false);
    }

    private void onLoadMord() {
        limit = ++limit;
        Map<String, String> map = new HashMap<>();
        map.put("sdk_version", "1");
        map.put("limit", limit + "");
        HttpCom.POST(mhandler, HttpCom.getGeneralize(), map, false);
    }

    /**
     * 刚进来请求
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<Generalize> pinglunBeen = HttpUtils.DNSGeneralize(msg.obj.toString());
                    if (pinglunBeen != null) {
                        dataList.addAll(pinglunBeen);
                        adapter.setDataList(dataList);
                        MCUtils.calculateListViewHeight(lv_generalize);
                    } else {
                        springview.setVisibility(View.GONE);
                        ToastUtil.showToast("没有数据啊！");
                    }
                    break;
                case 2:
                    springview.setVisibility(View.GONE);
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        lv_generalize.setFocusable(false);
        MCUtils.calculateListViewHeight(lv_generalize);
        MobclickAgent.onPageStart("GeneralizeFragment");
    }

    /**
     * 加载更多请求
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<Generalize> dataList = HttpUtils.DNSGeneralize(msg.obj.toString());
                    if (dataList != null) {
                        GeneralizeFragment.this.dataList.addAll(dataList);
                        adapter.setDataList(GeneralizeFragment.this.dataList);
                        lv_generalize.setFocusable(false);
                        MCUtils.calculateListViewHeight(lv_generalize);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:
                    break;
            }
        }
    };


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_text://明细
                UserInfo loginUser = Utils.getLoginUser();
                if (loginUser == null)
                    startActivity(new Intent(getActivity(), LoginActivity.class));//我的邀请
                else
                    startActivity(new Intent(getActivity(), MyInvitationActivity.class));//我的邀请
                break;
            case R.id.tv_activityDetail://活动详情
                startActivity(new Intent(getActivity(), ActivityRules.class));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent();
        intent.putExtra("id", dataList.get(i).game_id + "");
        intent.setClass(getActivity(), GameDetActivity.class);
        startActivity(intent);
    }
}
